package com.shushan.pages;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.home.ChildMainFragment;
import com.shushan.lockscreen.bean.AppInfo;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.SystemUtils;
import com.shushan.util.ToolUtils;
import com.shushan.util.UmengEventUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;


/**
 * @ClassName: VideoPageActivity
 * @Desciption: //首次进入APP的第一个页面，播放视频
 * @author: zhangshihao
 * @date: 2018-04-02
 */
public class VideoPageActivity extends BaseActivity implements MediaPlayer.OnCompletionListener, View.OnClickListener,
        SurfaceHolder.Callback {

    private static final String TAG = "VideoPageActivity";

    private SurfaceView svStart;
    private MediaPlayer player;
    private SurfaceHolder holder;
    private Button btnBeginExplore;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i(TAG, "onCreate()");

        setContentView(R.layout.activity_video_page);
        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true, this);
        initView();

        new GetAppListTask().execute();
    }

    private void initView() {
        svStart = findViewById(R.id.sv_start);
        initMediaPlay();
        btnBeginExplore = (Button) findViewById(R.id.btn_begin_explore);
        btnBeginExplore.setOnClickListener(this);
    }

    private void initMediaPlay() {
        holder = svStart.getHolder();
        holder.addCallback(this);
        holder.setKeepScreenOn(true);
        player = new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);

        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (!player.isPlaying()) {
                    player.start();
                }
            }
        });
        try {
            AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.start_movie);
            player.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
                    file.getLength());
            player.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
            player.setLooping(true);
            player.prepare();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!player.isPlaying()) {
            Log.i(TAG, "onStart : play()");
            player.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
        player.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player.isPlaying()) {
            player.stop();
        }
        player.release();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_begin_explore) {
            startActivity(GuideActivity.class);
            finish();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        player.setDisplay(holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    private class GetAppListTask extends AsyncTask<Void,Void,List<AppInfo>> {

        @Override
        protected List<AppInfo> doInBackground(Void... voids) {
            List<AppInfo> appInfoList = SystemUtils.getUsePackages(VideoPageActivity.this);

            return appInfoList;
        }

        @Override
        protected void onPostExecute(List<AppInfo> appInfoList) {
            if(!ToolUtils.isListEmpty(appInfoList)){
                for (AppInfo appInfo : appInfoList){
                    if(appInfo != null && !TextUtils.isEmpty(appInfo.getPackageName())
                            && appInfo.getPackageName().startsWith("com.shushan.")
                            &&!(appInfo.getPackageName().equals(Constants.HUAWEI_PACKAGE_NAME)
                            || appInfo.getPackageName().equals(Constants.MASTER_PACKAGE_NAME))){
                        Log.i(TAG,"onPostExecute=" + appInfo.getPackageName());
                        UmengEventUtil.onEvent(BabyApplication.getInstance(),"split_install_click_id");
                        break;
                    }
                }
            }
        }
    }
}
